package com.example.homework_03;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import java.util.HashSet;

//复择对话框
public class MixChoiceDialog extends DialogFragment {

    private String[] color = {"red", "blue", "yello"};
    private MixChoiceListener listener;
    private AlertDialog.Builder dialog;
    private HashSet selectedItem;

    MixChoiceDialog(Context context, MixChoiceListener listener) {
        selectedItem = new HashSet();
        setListener(listener);
    }

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("选择对话框")
                .setMultiChoiceItems(color, new boolean[3], new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            selectedItem.add(color[which]);
                        } else {
                            selectedItem.remove(color[which]);
                        }
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onClicked(selectedItem);
            }
        });
        return builder.create();
    }

    public interface MixChoiceListener {
        public void onClicked(HashSet colorset);
    }

    public void setListener(MixChoiceListener listener) {
        this.listener = listener;
    }

}
